<template>
  <div style="padding: 2rem;">
    <div style="display: flex; justify-content: center; align-items: center;">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="产品名称" style="margin-bottom: 0;">
          <el-input style="width: 145px;" v-model="searchForm.productName" placeholder="请输入产品名称"></el-input>
        </el-form-item>
        <el-form-item label="价格" style="margin-bottom: 0;">
          <el-input style="width: 145px;" v-model.number="searchForm.price" placeholder="请输入价格"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="客户ID" style="margin-bottom: 0;">
          <el-input style="width: 145px;" v-model.number="searchForm.customerid" placeholder="请输入客户ID"
            type="number"></el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 0; margin-right: 12px;">
          <el-button type="primary" @click="handleSearch" plain>查询</el-button>
        </el-form-item>
      </el-form>
      <el-button type="success" @click="openDialog('add')" plain>新建</el-button>
      <el-button type="danger" @click="handleBatchDelete" plain>批量删除</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%; margin-top: 15px;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="120" />
      <el-table-column prop="orderid" label="订单ID" width="120" />
      <el-table-column prop="productName" label="产品名称" width="120" />
      <el-table-column prop="price" label="价格" width="120" />
      <el-table-column prop="customerid" label="客户ID" width="120" />
      <el-table-column prop="nickname" label="客户名称" width="120" />
      <el-table-column prop="addr" label="客户地址" width="120" />
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="openDialog('edit', scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row.orderid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container" style="width: 100%;">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10]" :size="size"
        :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <el-dialog :title="dialogTitle" v-model="dialogVisible" align-center>
      <el-form :model="addForm" :rules="rules" ref="addFormRef" label-width="100px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="addForm.productName"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model.number="addForm.price"></el-input>
        </el-form-item>
        <el-form-item label="客户ID" prop="customerid">
          <el-input v-model.number="addForm.customerid"></el-input>
        </el-form-item>
        <el-form-item label="客户名称" prop="nickname">
          <el-input v-model="addForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" prop="addr">
          <el-input v-model="addForm.addr"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import customerApi from './api/customer'
import orderApi from '../api/order'

const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(false)
const disabled = ref(false)
const tableData = ref([])
const total = ref(0)
const searchForm = ref({
  productName: '',
  price: null,
  customerid: null
})
const dialogVisible = ref(false)
const dialogTitle = ref('添加订单')
const addForm = ref({
  orderid: null,
  productName: '',
  price: null,
  customerid: null,
  nickname: '',
  addr: ''
})
const rules = {
  productName: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
  price: [
    { required: true, message: '请输入产品价格', trigger: 'blur' },
    { type: 'number', message: '输入非法', trigger: 'blur' }
  ],
  customerid: [
    { required: true, message: '请输入客户ID', trigger: 'blur' },
    { type: 'number', message: '输入非法', trigger: 'blur' }
  ],
  nickname: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
  addr: [{ required: true, message: '请输入客户地址', trigger: 'blur' }]
}
const addFormRef = ref(null)
const selectedOrders = ref([])

const openDialog = (type, row = null) => {
  if (type === 'add') {
    dialogTitle.value = '添加订单'
    addForm.value = {
      orderid: null,
      productName: '',
      price: null,
      customerid: null,
      nickname: '',
      addr: ''
    }
  } else if (type === 'edit' && row) {
    dialogTitle.value = '编辑订单'
    addForm.value = { ...row }
  }
  dialogVisible.value = true
}

const handleSave = async () => {
  addFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (addForm.value.orderid) {
          await orderApi.update(addForm.value)
          ElMessage.success('订单更新成功')
        } else {
          await orderApi.add(addForm.value)
          ElMessage.success('订单增加成功')
        }
        dialogVisible.value = false
        handleSearch()
      } catch (error) {
        ElMessage.error('操作失败')
      }
    }
  })
}

const handleDelete = async (orderid) => {
  try {
    await orderApi.delete(orderid)
    ElMessage.success('订单删除成功')
    handleSearch()
  } catch (error) {
    ElMessage.error('删除失败')
  }
}

const handleBatchDelete = async () => {
  if (selectedOrders.value.length === 0) {
    ElMessage.warning('请选择要删除的订单')
    return
  }
  const orderids = selectedOrders.value.map(order => order.orderid).join(',')
  try {
    await orderApi.deleteBatch(orderids)
    ElMessage.success('订单批量删除成功')
    handleSearch()
  } catch (error) {
    ElMessage.error('批量删除失败')
  }
}

const handleSelectionChange = (selection) => {
  selectedOrders.value = selection
}

const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

const handleSearch = async () => {
  try {
    const response = await orderApi.findOrderByPage(currentPage.value, pageSize.value)
    total.value = response.data.total
    tableData.value = response.data.rows
  } catch (error) {
    ElMessage.error('查询失败')
  }
}

onMounted(async () => {
  handleSearch()
})
</script>

<style>
#app {
  display: flex;
  justify-content: center;
}
</style>
